package com.wclass.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.wclass.entity.BaseAttachment;
import com.wclass.mapper.BaseAttachmentMapper;
import com.wclass.service.IBaseAttachmentService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 附件 服务实现类
 * </p>
 *
 */
@Service
public class BaseAttachmentServiceImpl extends ServiceImpl<BaseAttachmentMapper, BaseAttachment> implements IBaseAttachmentService {

    @Override
    public List<BaseAttachment> selectListByTheirIdAndType(Long id, String type) {
        EntityWrapper<BaseAttachment> ew = new EntityWrapper<>();
        ew.eq("their_id", id).eq("type", type);
        return baseMapper.selectList(ew);
    }

    /**
     * 绑定附件所属
     *
     * @param ids
     * @param theirId
     * @param type
     * @return
     */
    @Override
    public Integer upTheirByIds(Long[] ids, Long theirId, String type) {
        //解除历史附件绑定
        baseMapper.fileUnbind(theirId, type);

        if (ids == null || ids.length == 0) {
            return 0;
        }

        //绑定新附件
        BaseAttachment info = new BaseAttachment();
        info.setTheirId(theirId);

        return baseMapper.update(info, new EntityWrapper<BaseAttachment>().in("id", ids));
    }

}
